﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Security.AccessControl;

namespace nb.development.csmediamanager
{
    public partial class MySites : Form
    {
        public MySites()
        {
            InitializeComponent();
            foreach (Site site in Sites.Instance.Values)
            {
                addNewTab(site.Id, site.Name, site.Url, site.User, site.Key);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (Site si in Sites.Instance.Values)
                if (!tabControl1.TabPages.ContainsKey(si.Id.ToString()))
                    si.Delete();
            Sites s = Sites.Instance;
            foreach (TabPage tp in tabControl1.TabPages)
                if (tabControl1.TabPages.IndexOf(tp) > 0)
                    if (s.ContainsKey(new Guid(tp.Name)))
                    {
                        s[new Guid(tp.Name)].Name = tp.Controls[0].Controls[0].Text;
                        s[new Guid(tp.Name)].Url =  tp.Controls[0].Controls[2].Text;
                        s[new Guid(tp.Name)].User = tp.Controls[0].Controls[4].Text;
                        s[new Guid(tp.Name)].Key = tp.Controls[0].Controls[6].Text;
                        s[new Guid(tp.Name)].Save();
                    }
                    else
                        s.Add(tp.Controls[0].Controls[2].Text, tp.Controls[0].Controls[4].Text, tp.Controls[0].Controls[6].Text, tp.Controls[0].Controls[0].Text);
            s.SaveAll();
        }

        private void addNewTab(Guid id, string name, string url, string user, string key)
        {
            TabPage tp = new TabPage(name);
            TableLayoutPanel tlp = new TableLayoutPanel();
            tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30));
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 70));
            VistaControls.TextBox tb1 = new VistaControls.TextBox();
            tb1.Text = name;
            tb1.Name = "txtName" + tabControl1.TabPages.Count;
            tb1.KeyUp += new KeyEventHandler(txtName0_KeyUp);
            tb1.Dock = DockStyle.Fill;
            tlp.Controls.Add(tb1, 1, 0);
            Label l = new Label();
            l.Text = "Name:";
            l.Dock = DockStyle.Fill;
            l.TextAlign = ContentAlignment.MiddleRight;
            tlp.Controls.Add(l, 0, 0);
            VistaControls.TextBox tb2 = new VistaControls.TextBox();
            tb2.Name = "txtUrl" + tabControl1.TabPages.Count;
            tb2.Dock = DockStyle.Fill;
            tb2.Text = url;
            tlp.Controls.Add(tb2, 1, 1);
            Label l2 = new Label();
            l2.Text = "Url:";
            l2.Dock = DockStyle.Fill;
            l2.TextAlign = ContentAlignment.MiddleRight;
            tlp.Controls.Add(l2, 0, 1);
            VistaControls.TextBox tb3 = new VistaControls.TextBox();
            tb3.Name = "txtUser" + tabControl1.TabPages.Count;
            tb3.Dock = DockStyle.Fill;
            tb3.Text = user;
            tlp.Controls.Add(tb3, 1, 2);
            Label l3 = new Label();
            l3.Text = "Username:";
            l3.Dock = DockStyle.Fill;
            l3.TextAlign = ContentAlignment.MiddleRight;
            tlp.Controls.Add(l3, 0, 2);
            VistaControls.TextBox tb4 = new VistaControls.TextBox();
            tb4.Name = "txtKey" + tabControl1.TabPages.Count;
            tb4.Text = key;
            tb4.Dock = DockStyle.Fill;
            tlp.Controls.Add(tb4, 1, 3);
            Label l4 = new Label();
            l4.Text = "API Key:";
            l4.Dock = DockStyle.Fill;
            l4.TextAlign = ContentAlignment.MiddleRight;
            tlp.Controls.Add(l4, 0, 3);
            VistaControls.Button btn = new VistaControls.Button();
            btn.Text = btnDelete.Text;
            btn.Name = btnDelete.Name + tabControl1.TabPages.Count;
            btn.Click += new EventHandler(btnDelete_Click);
            tlp.Controls.Add(btn, 1, 4);
            tlp.Dock = DockStyle.Fill;
            tp.Controls.Add(tlp);
            tp.BackColor = Color.White;
            tp.Name = id.ToString();
            tabControl1.TabPages.Add(tp);
        }

        private void txtName0_KeyUp(object sender, KeyEventArgs e)
        {
            VistaControls.TextBox tb = sender as VistaControls.TextBox;
            if (int.Parse(tb.Name.Remove(0, 7)) == 0)
            {
                addNewTab(Guid.NewGuid(), tb.Text, "", "", "");
                tabControl1.SelectedIndex = tabControl1.TabPages.Count - 1;
                tabControl1.SelectedTab.Controls[0].Controls[0].Focus();
                ((VistaControls.TextBox)tabControl1.SelectedTab.Controls[0].Controls[0]).Select(1, 0);
                tb.Text = "";
            }
            else tabControl1.TabPages[int.Parse(tb.Name.Remove(0, 7))].Text = tb.Text;
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                VistaControls.Button btn = sender as VistaControls.Button;
                tabControl1.SelectedIndex--;
                foreach (TabPage tb in tabControl1.TabPages)
                    if (tb.Controls[0].Controls.Contains(btn))
                        tabControl1.TabPages.Remove(tb);
            }
        }
    }
}
